package pl.enigmatic.reflection;

import java.lang.reflect.Method;

import pl.enigmatic.util.Reflection;


public class SimpleGetter implements Getter {

	private final Method method;
	private final PropertyPath path;

	public SimpleGetter(final Class<?> cls, final String property) throws SecurityException, NoSuchMethodException {
		this.path = new PropertyPath(property);
		method = cls.getMethod(Reflection.getter(cls, property), new Class<?>[] {});
	}

	@Override
	public Object get(final Object target) {
		try {
			return method.invoke(target);
		} catch (final Exception e) {
			return null;
		}
	}

	@Override
	public Class<?> getType() {
		return method.getReturnType();
	}

	@Override
	public PropertyPath getPath() {
		return path;
	}

}
